﻿@{
    ViewBag.Title = "巡检单" + ViewBag.TicketID + ViewBag.PagName;
    Layout = "~/Views/Shared/_LayoutKoJSMobile.cshtml";
}
<div class="aux-body">
    <div class="aux-tabcontent">
                                               @* <button id="DivPull" onclick="retract(event)" style="display:inline-block">收</button>*@
        <div class="fx-grid_noborder">
            <div id="fx-scrollbar">
                <!-- style变化 height:650px; -->
                <div id="fx-scrollbar-scrollable" style="width: 100%;">
                    <!-- style变化 -->
                    <div class="fx-grid-container fx-scrollbar-content" style="overflow: visible; width: 100%;">
                        <!-- style变化 -->
                        <div class="quickStarttab" style="height: 175px">
                            <form id="KSForm">
                                <div id="Ticketfield">
                                    <div class="quickStarttab-item">
                                        <label>巡检单名称：</label>
                                        <input type="text" name="TcChr_Name" id="TcChr_Name">
                                    </div>
                                    <div class="quickStarttab-item">
                                        <label>巡检单ID：</label>
                                        <input type="text" name="TaskID" id="TaskID" value="" readonly="true" style="background-color: #ECECEC;">
                                    </div>
                                    <div id="DivHide">
                                    <div class="quickStarttab-item" style="width: 100%;">
                                        <label>巡检单描述：</label>
                                        <input type="text" name="TcChr_Description" id="TcChr_Description" class="input_all">
                                    </div>
                                    <div class="quickStarttab-item">
                                        <label>创建模板时间：</label>
                                        <input type="text" name="cDat_TicketTime" id="cDat_TicketTime" readonly="true" style="background-color: #ECECEC;">
                                    </div>
                                    <div class="quickStarttab-item">
                                        <label>生成巡检单时间：</label>
                                        <input type="text" name="CreateDate" id="CreateDate" readonly="true" style="background-color: #ECECEC;">
                                    </div>
                                    <div class="quickStarttab-item">
                                        <label>状态：</label>
                                        <select  name="Status" id="Status" style="width: 272px;background-color: #ECECEC;" >
                                            <option value="1">进行中</option>
                                            <option value="2">已取消</option>
                                            <option value="3">已完成</option>
                                        </select>
                                    </div>
                                    <div class="quickStarttab-item">
                                        <label>执行人：</label>
                                        <input type="text" name="TcChr_FullName" id="TcChr_FullName" value="@(((Dictionary<String, String>)ViewBag.UserPreferences)["FullName"])" readonly="true" style="background-color:#ECECEC;">
                                    </div>
                                    <div class="quickStarttab-item">
                                        <label>最后更新时间：</label>
                                        <input type="text" name="ModifiedDate" id="ModifiedDate" value="" readonly="true" style="background-color: #ECECEC;">
                                    </div>
                                    </div>
                                 @*   <div style="float: right; display: inline-block; margin-right: 42px; margin-top: -32px">
                                        <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="FormSave" type="submit"><span class="ui-button-text">Save</span></button>
                                    </div>*@
                                     <div style="float: right; display:none; margin-right: 42px; margin-top: -32px">
                                        <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="FormSave" type="submit"><span class="ui-button-text">Save</span></button>
                                    </div>
@*                                    <div class="quickStarttab-item" style="width: 100%;">
                                        <div style="float: right; display: inline-block; margin-right: 42px;">
                                            <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="FormSave" type="submit"><span class="ui-button-text">Save</span></button>
                                        </div>
                                    </div>*@
                                </div>
                            </form>
                        </div>
                        <div>
                            <h2 class="itemtitle2" style="display:inline">待巡检对象</h2>
                            <a  id="buginfo" style="margin-right: 42px;width: 90px; height: 5px;" onclick="BugInfoList()" href="#"></a>
                            @*<label id="buginfo" style="display:none;margin-right: 42px;width: 70px; height: 35px;" onclick="BugInfoList()">巡检单异常信息</label>*@
                            <button id="DivPull" onclick="retract(event)" style="float:right;background:url(../../Content/images/BtnHide.png); zoom:60%; width:22px; height:24px; border:0;outline:none;"></button>
                        </div>
                        <!-- table -->
                        <div>
                            <table id="gridTable1" style="width: 897px;"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="DialogRD" style="height: 1000px"></div>
<div id="AlertMessage">
    <p id="AlertMessageBody" class="msgbody"></p>
</div>
<div id="ConfirmMessage">
    <p id="ConfirmMessageBody" class="msgbody"></p>
</div>

@section scripts {
    <script src="~/Scripts/zhaoyangdeerzi.js"></script>
    @*<script src="public/lib/jquery/js/i18n/grid.locale-cn.js" type="text/javascript"></script> <script src="~/Scripts/jquery.jqGrid.locale-en.js"></script>*@
    <script type="text/javascript">
        var pagenameinfo = "@ViewBag.pagename";
        var DivStatus = "Show";
        if (pagenameinfo == "TaskFrame" || pagenameinfo=="") {
            $("#LayoutMyTask").addClass("selected");
            document.getElementById("SaveBtn").style.display = 'block';
            document.getElementById("SaveBtnsub").style.display = 'block';
            document.getElementById("DeleteBtn").style.display = 'block'; 
        }
        if (pagenameinfo == "Search") {
            $("#LayoutSearch").addClass("selected");          
        }
        //保存
        $("#SaveBtn").click(function () {
            //$('#Status').val("进行中");
            $("#FormSave").click();
        });

        //提交
        $("#SaveBtnsub").click(function () {
            //$('#Status').val("已完成");
            $("#Status ").get(0).selectedIndex = 2;
            document.getElementById("SaveBtn").style.display = 'none';
            document.getElementById("SaveBtnsub").style.display = 'none';
            document.getElementById("DeleteBtn").style.display = 'none';
            $("#FormSave").click();
        });

        //删除
        $("#DeleteBtn").click(function () {
            ShowMsg('是否确定删除?', function (yes) {
                if (yes) {
                    $.ajax("/API/InspectionTaskAPI?TaskID=" + '@ViewBag.TicketID', {
                        type: "PUT", contentType: "application/json",
                        success: function (data) {
                            window.location.href = "/Home/MyTask";
                        }
                    });
                }
            });
        });
        var DataList = {};
        var DataRequestID = "";
        var IsTcChr_TicketID = "";
        $(document).ready(function () {
            $.getJSON("/API/InspectionTaskFrameAPI?TaskID=" + "@ViewBag.TicketID", function (data, textStatus, jqXHR) {
                $("#Ticketfield :input").each(function (index, element) {
                    $('#' + element.id + '').val(data['' + element.id + '']);
                });
                DataRequestID = data.RequestID;
                var StatusText = data.Status;
                IsTcChr_TicketID = data.TcChr_TicketID;
                if (StatusText == "3")
                {
                    //3代表已完成
                    document.getElementById("SaveBtn").style.display = 'none';
                    document.getElementById("SaveBtnsub").style.display = 'none';
                    document.getElementById("DeleteBtn").style.display = 'none';
                }
                BugInfoList();
                //去除时间空间上的字符
                $('input[id$="Date"]').add($('input[id$="Time"]')).each(function () {
                    this.value = $(this).val().replace("T", " ").replace("+08:00", "");
                })
            });

            //提交页面
            $('#KSForm').bind('submit', function (event) {
                event.preventDefault();
                var data2list = [];
                for (var i = 0; i < DataList.length; i++) {
                    if (DataList[i].ChangeFlag != null && DataList[i].ChangeFlag != "") {
                        var data2 = {};
                        data2['RequestID'] = DataList[i].RequestID;
                        data2['RcChr_Char01'] = DataList[i].RcChr_Char01;
                        if (DataList[i].RcChr_Char02 != "unchanged") {
                            data2['RcChr_Char02'] = DataList[i].RcChr_Char02;
                        }
                        data2list.push(data2);
                    }
                }

                $.ajax("/API/InspectionTaskAPI?id=" + '@ViewBag.TicketID', {
                    data: JSON.stringify(data2list),
                    type: "PUT", contentType: "application/json",
                    async: false,
                    success: function (data) {
                    }
                });


                ajaxModify(this, function (data) {
                    alert(data);
                    LoadHosts();
                }, "/API/InspectionTaskFrameAPI?id=" + DataRequestID);
                return false;
            });

            $("#gridTable1").jqGrid({
            @*url: '/API'+'@Url.Action("GetRule", "InspectionRuleAPI")',
            datatype: 'json',*@
                //treeGrid: true,//w为ture 则为树形表格
                //treeGridModel: "adjacency",
                //ExpandColumn: "类型",//展开的列
                //ExpandColClick: true,// 树形表格是否展开
            //    grouping : true , // 是否分组,默认为false
            //    groupingView : {
            //        groupField: ['OcChr_DeviceName'], // 按照哪一列进行分组
            //        groupColumnShow : [ true ], // 是否显示分组列
            //        groupText : [ '<b>{0} - {1} 条记录</b>' ], // 表头显示的数据以及相应的数据量
            //        groupCollapse : false , // 加载数据时是否只显示分组的组信息
            //        groupDataSorted : true , // 分组中的数据是否排序
            //        groupOrder : [ 'asc' ], // 分组后的排序
            //        groupSummary : [ true ], // 是否显示汇总.如果为true需要在colModel中进行配置
            //        summaryType : 'max' , // 运算类型，可以为max,sum,avg</div>
            //        summaryTpl : '<b>Max: {0}</b>' ,
            //        showSummaryOnHide : true, //是否在分组底部显示汇总信息并且当收起表格时是否隐藏下面的分组
            //},
                url: '',
                datatype: 'local',
                rowNum: 500,
                colNames: ['ID', '名称', '类型', '对象属性说明', '指标', '检测值', 'RcChr_Char01','备注','阀值', 'ChangeFlag', 'RcChr_RuleValue'],
                colModel: [
                          { index: "1", name: "RequestID", sorttype: "text", hidden: true },
                          { index: "536870935", name: "OcChr_DeviceName", sorttype: "text",width:100 },
                          { index: "536870931", name: "OcChr_Classification", sorttype: "text", width: 50 },
                          { index: "536870943", name: "OcChr_Char032", sorttype: "text", width: 140},
                          { index: "536870928", name: "RcChr_RuleName", sorttype: "text",width:200 },
                          { index: "536870929", name: "RcChr_RuleType", sorttype: "text", formatter: formatType ,width:100},
                          { index: "536870923", name: "RcChr_Char01", sorttype: "text", hidden: true },
                          { index: "536870924", name: "RcChr_Char02", sorttype: "text", width: 75, formatter: formatDescription, hidden: true },
                          { index: "536870926", name: "RcChr_Threshold", sorttype: "text",width:50 },
                          { index: "", name: "ChangeFlag", sorttype: "text", hidden: true },
                          { index: "536870922", name: "RcChr_RuleValue", sorttype: "text", hidden: true },
                ],
                //treeGrid: true,
                //ExpandColClick: true,
                //ExpandColumn: 'OcChr_Classification',
                viewrecords: true,
                jsonReader: {
                    repeatitems: false,
                    id: "rule"
                },
                mtype: "GET",
                //autowidth: true,
                //shrinkToFit: true,
                sortname: 'Request',
                sortorder: 'asc',
                postData: {
                    Permission: 'public',
                },
                prmNames: {
                    id: 'Type'
                },
                scrollOffset: 0,
                height: "250",
                width: "837",
                viewrecords: true,
                gridview: true,

                beforeSelectRow: function (rowid, status, e, rowObject) {
                    return false;
                },
                ondblClickRow: function () {
                },
                gridComplete: function () {
                },
                loadComplete: function () {
                    DataList = $('#gridTable1').jqGrid('getRowData');
                    $(".RcChr_Char01Input").bind('input propertychange', setChangeFlag);
                    $(".RcChr_Char02Input").bind('input propertychange', setChangeFlag);
                    $(".RcChr_Char01DropDown").bind('click', setChangeFlag);
                    $(".RcChr_Char01Radio").bind('click', setChangeFlag);
                    $(".RcChr_Char01btn").bind('click', setChangeFlag);
                    SetColor();
                }
            });
            LoadHosts();

        });

        //刷新页面
        function LoadHosts() {
            var data = {};
            jQuery("#gridTable1").jqGrid("setGridParam", {
                url: "/API/InspectionTaskAPI/GetbyID?TicketID=@ViewBag.TicketID", //设置表格的url
                datatype: "json", //设置数据类型
                //postData: { strJson: data }
            }).trigger("reloadGrid");
        }

            //不同的控件赋值
            function setChangeFlag() {
                if (this.type == "radio") {
                    $("input[name=" + this.name + "]").each(function () {
                        if (this.checked == true) {
                            DataList[this.parentElement.parentElement.id - 1].RcChr_Char01 = this.value;
                            DataList[this.parentElement.parentElement.id - 1].RcChr_Char02 = "unchanged";
                            DataList[this.parentElement.parentElement.id - 1].ChangeFlag = Date.now();
                            //$("#gridTable1").setCell(this.parentElement.parentElement.parentElement.id, 'ChangeFlag', Date.now());
                            //$("#gridTable1").setCell(this.parentElement.parentElement.parentElement.id, 'RcChr_Char01', this.value);
                        }
                    });
                }
                else if (this.type == "text") {
                    if (this.className == "RcChr_Char02Input") {
                        DataList[this.parentElement.parentElement.id - 1].RcChr_Char02 = this.value;
                        DataList[this.parentElement.parentElement.id - 1].ChangeFlag = Date.now();
                    }
                    else {
                        DataList[this.parentElement.parentElement.id - 1].RcChr_Char01 = this.value;
                        DataList[this.parentElement.parentElement.id - 1].RcChr_Char02 = "unchanged";
                        DataList[this.parentElement.parentElement.id - 1].ChangeFlag = Date.now();
                    }
                }
                else if (this.type == "select-one") {
                    DataList[this.parentElement.parentElement.id - 1].RcChr_Char01 = this.value;
                    DataList[this.parentElement.parentElement.id - 1].RcChr_Char02 = "unchanged";
                    DataList[this.parentElement.parentElement.id - 1].ChangeFlag = Date.now();
                }
                else if (this.type == "submit") {
                    DataList[this.parentElement.parentElement.id - 1].RcChr_Char01 = this.value;
                    DataList[this.parentElement.parentElement.id - 1].RcChr_Char02 = "unchanged";
                    DataList[this.parentElement.parentElement.id - 1].ChangeFlag = Date.now();
                }
            }

            function formatDescription(cellvalue, options, rowObject) {
                var RcChr_Char02 = (rowObject.RcChr_Char02 == null ? "" : rowObject.RcChr_Char02);
                return '<input type="text" style="width:100px" class="RcChr_Char02Input" value="' + RcChr_Char02 + '"></input>'
            }

            //生成自定义控件
            function formatType(cellvalue, options, rowObject) {
                if (options.rowId > 0) {
                    var type = rowObject.RcChr_RuleType;
                    var typevalue = rowObject.RcChr_RuleValue;
                    var ValueL = rowObject.RcChr_Char01 == null ? "" : rowObject.RcChr_Char01;
                    var returnStr = '';
                    if (typevalue != null) {
                        var valuesplit = typevalue.split(';');
                        var valuesplitlength = valuesplit.length;
                        switch (type) {
                            case 'Dropdownlist':
                                for (var i = 1; i <= valuesplitlength; i++) {
                                    if (valuesplit[i - 1] == ValueL)
                                        returnStr = returnStr + '<option  selected value="' + valuesplit[i - 1] + '">' + valuesplit[i - 1] + '</option>';
                                    else
                                        returnStr = returnStr + '<option  value="' + valuesplit[i - 1] + '">' + valuesplit[i - 1] + '</option>';
                                }
                                return '<select class="RcChr_Char01DropDown" name ="' + rowObject.RcChr_RuleRequestID + '">' + returnStr + '</select>';
                                //case 'CheckBox':
                                //    for (var i = 1; i < valuesplitlength; i++)
                                //        returnStr = returnStr + '<input class="RcChr_Char01" type="checkbox" name ="' + rowObject.RcChr_RuleName + rowObject.$id + '" value="' + valuesplit[i - 1] + '">' + valuesplit[i - 1] + '</input>';
                                //    return '<center>' + returnStr + '</center>';
                            case 'RadioBox':
                                for (var i = 1; i < valuesplitlength; i++) {
                                    //补个逻辑把radio附上去
                                    if (valuesplit[i - 1] == ValueL) {
                                        returnStr = returnStr + '<input class="RcChr_Char01Radio" style="display:none"  type="radio" checked name ="' + rowObject.RcChr_RuleRequestID + rowObject.$id + '" value="' + valuesplit[i - 1] + '"></input>';
                                        returnStr = returnStr + '<button class= "RcChr_Char01btn radiochecked" id="' + rowObject.RcChr_RuleRequestID + rowObject.$id + i + '"onclick="myRadio(this.id,\'' + rowObject.RcChr_RuleRequestID + rowObject.$id + '\',\'' + valuesplit[i - 1] + '\')" value="' + valuesplit[i - 1] + '">' + valuesplit[i - 1] + '</button>&nbsp;&nbsp;';
                                    }
                                    else {
                                        returnStr = returnStr + '<input class="RcChr_Char01Radio" style="display:none"  type="radio" name ="' + rowObject.RcChr_RuleRequestID + rowObject.$id + '" value="' + valuesplit[i - 1] + '"></input>';
                                        returnStr = returnStr + '<button class= "RcChr_Char01btn radiounchecked" id="' + rowObject.RcChr_RuleRequestID + rowObject.$id + i + '"onclick="myRadio(this.id,\'' + rowObject.RcChr_RuleRequestID + rowObject.$id + '\',\'' + valuesplit[i - 1] + '\')" value="' + valuesplit[i - 1] + '">' + valuesplit[i - 1] + '</button>&nbsp;&nbsp;';
                                    }
                                }
                                return returnStr;
                        }
                    }
                    else if (type == "Input")
                        returnStr = '<input type="text" class="RcChr_Char01Input" style="width:120px" name ="' + rowObject.RcChr_RuleRequestID + '" value="' + ValueL + '"></input>'
                    return returnStr;
                }
            }

            function myRadio(sId,name,value) {
                $("button[id='" + sId + "']").removeClass("radiounchecked").addClass("radiochecked");
                $("button[id='" + sId + "']").siblings("button").removeClass("radiochecked").addClass("radiounchecked");
                $("input[name='" + name + "'][value='" + value + "']").prop("checked", true);
                $("input[name='" + name + "'][value='" + value + "']").siblings("input").prop("checked", false);
                //为点击按钮添加选中状态
                var chked = $('input[name="' + name + '"]:checked').val();
            }
            function retract(event) {
                event.preventDefault();
                if (DivStatus == "Show") {
                    $("#DivHide").slideUp(500, function () { });
                    $($(".quickStarttab")[0]).animate({ height: "40px" });
                    //$("#TaskID").parent().hide();
                    DivStatus = "Hide";
                    //jQuery("#gridTable1").setGridHeight("370px");
                    $("#DivPull").css("backgroundImage", "url(../../Content/images/BtnShow.png)");
                    $("#fxshell-navpane").hide();
                    $(".aux-body")[0].setAttribute("style", "margin-left:0px");
                    $("#gridTable1").jqGrid('setGridWidth', 880);
                    jQuery("#gridTable1").setGridParam().showCol("RcChr_Char02");
                }
                else {
                    $("#DivHide").slideDown(500, function () { });
                    $($(".quickStarttab")[0]).animate({ height: "175px" });
                    $("#TaskID").parent().show();
                    DivStatus = "Show";
                    //jQuery("#gridTable1").setGridHeight("250px");
                    $("#DivPull").css("backgroundImage", "url(../../Content/images/BtnHide.png)");
                    $("#fxshell-navpane").show();
                    $(".aux-body")[0].setAttribute("style", "margin-left:13%");
                    $("#gridTable1").jqGrid('setGridWidth', 930);
                    jQuery("#gridTable1").setGridParam().hideCol("RcChr_Char02");
                }
            }

            function SetColor() {
                var rowDatas = $('#gridTable1').jqGrid('getRowData');
                for (i = 0; i < rowDatas.length; i++) {
                    var rowData = rowDatas[i];
                    var data = rowData.RcChr_RuleValue.split(";");
                    if (data.length > 1) {
                        if (rowData.RcChr_Char01 != data[0] && rowData.RcChr_Char01!="") {
                            $("#gridTable1").jqGrid('setRowData', i + 1, false, { background: '#ffff80' });
                        }
                    }
                }
                return true;
            }

            $("#DialogRD").dialog({
                autoOpen: false,
                height: 200,
                width: 350,
                title: "异常信息：详情请点击",
                //modal:true,
                open: function (event, ui) {
                },
                close: function () {
                    //resourceDescriptionModel.Items([new Server("Virtual", null, null)]);
                }
            });
            
            function BugInfoList() {   
                var IsCreateDate = $('#CreateDate').val().replace("T", " ").replace("+08:00", "");
                document.getElementById('DialogRD').innerHTML = "";
                $.getJSON("/API/InspectionTaskAPI/GetSearchByList?TcChr_TicketID=" + IsTcChr_TicketID + "&CreateDateBug=" + IsCreateDate, function (data) {
                    if (data.length > 0) {
                        $("#buginfo").text('巡检单异常信息');
                        $("#buginfo").css({ "color": "red" });
                        $("#DialogRD").dialog("open");
                        //document.getElementById("buginfo").style.color = 'red';
                        var a = "";
                        for (var i = 0; i < data.length; i++) {
                            a = document.createElement("a");
                            //a.onclick = function () {
                            //a.href = window.open("/InspectionTask/AbnormalIndex?GTicketID=" + data[i].TaskID + "&PageName=" + pagenameinfo);
                            //$("#DialogRD").dialog("close");
                            //};
                            a.target = "_blank"
                            a.id = "a" + i;
                            a.href = "/InspectionTask/AbnormalIndex?GTicketID=" + data[i].TaskID + "&PageName=" + pagenameinfo;
                            a.innerText = i + 1 + "." + data[i].TcChr_FullName + " |" + data[i].TcChr_Name + " |" + data[i].CreateDate.replace("T", " ").replace("+08:00", "") + "\n";
                            document.getElementById("DialogRD").appendChild(a);
                            // a += "<a id='a" + i + "' href='http:localhost:2470/InspectionTask/Index?GTicketID=" + data[i].TcChr_TicketID + '&PageName=' + "TaskFrame" + "'>我是超链接" + i + "</a>";
                        }
                    }
                    else {
                        //$("#buginfo").text('无异常');
                        //$("#buginfo").css({ "color": "#00e300" });
                    }
                })
            }
    </script>
}